﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using log4net;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._02_User
{
    public partial class UpdateUserForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(UpdateUserForm));
        private bool _isReferenceValidated = false;

        public int UserId { get; set; }
        private UserInfoAndReferenceUserInfo UserInfo { get; set; } = new UserInfoAndReferenceUserInfo();

        public UpdateUserForm()
        {
            InitializeComponent();
        }

        private void UpdateUserForm_Load(object sender, EventArgs e)
        {

        }

        private void UpdateUserForm_Shown(object sender, EventArgs e)
        {
            try
            {
                UserInfo = UserService.QueryByUserId(UserId);
                textBox2.Text = UserInfo.UserInfo.Phone;
                textBox1.Text = UserInfo.UserInfo.Name;
                label4.Text = UserInfo.UserInfo.InvitationCode;
                textBox3.Text = UserInfo.UserInfo.ReferenceInvitationCode;
                label7.Text = UserInfo.ReferenceUserInfo.Name;
                _isReferenceValidated = true;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(textBox3.Text.Trim()))
                {
                    var ui = UserService.QueryByInvitationCode(textBox3.Text.Trim());
                    if (ui != null)
                    {
                        _isReferenceValidated = true;
                        UICommonTool.ShowInfoDialog("验证成功！");
                        label7.Text = ui.Name;
                    }
                    else
                    {
                        _isReferenceValidated = false;
                        UICommonTool.ShowAlertDialog("验证失败，推荐人邀请码错误！");
                        label7.Text = "推荐人邀请码错误！";
                    }
                }
                else
                {
                    UICommonTool.ShowAlertDialog("推荐人邀请码不能为空！");
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                if (UICommonTool.ShowConfirmDialog("确定要修改数据吗？"))
                {
                    UserInfo.UserInfo.Phone = textBox2.Text.Trim();
                    UserInfo.UserInfo.Name = textBox1.Text.Trim();
                    UserInfo.UserInfo.ReferenceInvitationCode = textBox3.Text.Trim();

                    try
                    {
                        bool b = UserService.UpdateUser2(UserInfo.UserInfo);
                        if (b)
                        {
                            UICommonTool.ShowInfoDialog("修改成功！");
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                        else
                        {
                            UICommonTool.ShowAlertDialog("修改失败！");
                        }
                    }
                    catch (MyException ex)
                    {
                        _logger.Error(ex);
                        UICommonTool.ShowAlertDialog(ex.Message);
                    }
                    catch (Exception ex2)
                    {
                        _logger.Error(ex2);
                        UICommonTool.ShowAlertDialog("网络错误！");
                    }
                }
            }
        }

        private bool ValidateForm()
        {
            if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("手机号码不能为空！");
                return false;
            }
            var reg = new Regex(@"[0-9]{11}");
            if (!reg.IsMatch(textBox2.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("手机号码格式不正确！");
                return false;
            }
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("姓名不能为空！");
                return false;
            }
            if (string.IsNullOrEmpty(textBox3.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("推荐人邀请码不能为空！");
                return false;
            }
            if (_isReferenceValidated == false)
            {
                UICommonTool.ShowAlertDialog("推荐人邀请码错误！");
                return false;
            }
            return true;
        }
    }
}
